package com.roc.dao.impl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.cardBin.ICardBinDao;
import com.roc.pojo.EtbSysCardBinno;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
@Repository
public class CardBinDaoImpl extends BaseDaoImpl implements ICardBinDao{

	@Override
	public void deleteCardBin(String cardBin, long cardLen)
			throws DaoException {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		try{
			Query query=session.createSQLQuery("delete from etb_syscardbinno e where e.CARDBIN="+cardBin+" and e.CARDLEN="+cardLen);
			query.executeUpdate();
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除是失败" + cardBin+","+cardLen, e);
		}
		
	}

	@Override
	public void updateCardBin(EtbSysCardBinno card,String cardBin,Long cardLen) throws DaoException {
		// TODO Auto-generated method stub
		 Session session=sessionFactory.getCurrentSession();
		    try{
		    	StringBuffer sql=new StringBuffer(" ");
		    	sql.append("update etb_syscardbinno e set ") ;
		    	if(!Constance.Null2EmptyTrim(card.getCardBin()).equals("")){
		    		sql.append(" e.CARDBIN="+card.getCardBin());
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardLen()).equals("")){
		    		sql.append(", e.CARDLEN="+card.getCardLen());
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardName()).equals("")){
		    		sql.append(", e.CARDNAME='"+card.getCardName()+"'");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getBankName()).equals("")){
		    		sql.append(",e.BANKNAME='"+card.getBankName()+"'");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getBranchId()).equals("")){
		    		sql.append(",e.BRANCHID="+card.getBranchId());
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardNo()).equals("")){
		    		sql.append(",e.CARDNO="+card.getCardNo());
		    	}
		    	sql.append(" where e.CARDBIN="+cardBin+" and e.CARDLEN="+cardLen);
				//session.createSQLQuery(sql.toString());
				SQLQuery query=session.createSQLQuery(sql.toString());
				query.executeUpdate();
		    }catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new DaoException("修改失败" + card.getCardBin()+","+card.getCardLen(), e);
			}

	}

	@Override
	public void addCardBin(EtbSysCardBinno card) throws DaoException {
		// TODO Auto-generated method stub
		 Session session=sessionFactory.getCurrentSession();
		    try{
		    	StringBuffer sql=new StringBuffer(" ");
		    	sql.append("insert into  etb_syscardbinno ( CARDBIN,CARDNAME,BANKNAME,BRANCHID,CARDLEN,CARDNO) values(") ;
		    	if(!Constance.Null2EmptyTrim(card.getCardBin()).equals("")){
		    		sql.append(card.getCardBin());
		    	}else{
		    		sql.append("null");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardName()).equals("")){
		    		sql.append(",'"+card.getCardName()+"'");
		    	}else{
		    		sql.append(",null");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getBankName()).equals("")){
		    		sql.append(",'"+card.getBankName()+"'");
		    	}else{
		    		sql.append(",null");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getBranchId()).equals("")){
		    		sql.append(","+card.getBranchId());
		    	}else{
		    		sql.append(",null");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardLen()).equals("")){
		    		sql.append(","+card.getCardLen());
		    	}else{
		    		sql.append(",null");
		    	}
		    	if(!Constance.Null2EmptyTrim(card.getCardNo()).equals("")){
		    		sql.append(","+card.getCardNo()+")");
		    	}else{
		    		sql.append(",null)");
		    	}
		    	//sql.append(" where e.CARDBIN="+card.getCardBin()+" e.CARDLEN="+card.getCardLen());
				//session.createSQLQuery(sql.toString());
				SQLQuery query=session.createSQLQuery(sql.toString());
				query.executeUpdate();
		    }catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new DaoException("修改失败" + card.getCardBin()+","+card.getCardLen(), e);
			}

	}

	@Override
	public EtbSysCardBinno queryCardBin(String cardBin, long cardLen)
			throws DaoException {
		// TODO Auto-generated method stub
		 Session session=sessionFactory.getCurrentSession();
		 EtbSysCardBinno card=null;
		 try{
			 SQLQuery query=session.createSQLQuery("select * from etb_syscardbinno e where e.cardLen="+cardLen+" and e.cardBin="+cardBin);
			 List<Object> obList=query.list();
			 Iterator iterator=obList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					EtbSysCardBinno tran=new EtbSysCardBinno();
					tran.setBankId((Constance.Null2EmptyTrim(obj[0]).toString()));
					tran.setIssusers(Constance.Null2EmptyTrim(obj[1]).toString());
					tran.setCardNo(Constance.Null2EmptyTrim(obj[2]).toString());
					tran.setCardLen(Long.parseLong(Constance.Null2EmptyTrim(obj[3]).toString()));
					tran.setCardBin(Constance.Null2EmptyTrim(obj[4]).toString());
					tran.setCardName(Constance.Null2EmptyTrim(obj[5]).toString());
					tran.setBankName(Constance.Null2EmptyTrim(obj[6]).toString());
					tran.setBranchId(Constance.Null2EmptyTrim(obj[7]).toString());
					tran.setBranchId2(Constance.Null2EmptyTrim(obj[8]).toString());
					tran.setCardType(Constance.Null2EmptyTrim(obj[9]).toString());
					tran.setCardOrg(Constance.Null2EmptyTrim(obj[10]).toString());
					//tran.setCardTag(Long.parseLong(Constance.Null2EmptyTrim(obj[11])));
					//tran.setCardTag2(Long.parseLong(Constance.Null2EmptyTrim(obj[12])));
					card=tran;
					//transList.add(tran);
				}
		 }catch(Exception e){
			 logger.error(e.getMessage(), e);
				throw new DaoException("修改失败" + card.getCardBin()+","+card.getCardLen(), e);
			}
			return card;
	}

	
	

}
